package tools;
/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

public class CategoryUtil {

	public static void main(String[] args) {
		
		

		/*String categoryTreeString = FileUtil
				.readFileByLines("/home/dsw/workspaces/workspace07/operator1019/src/main/webapp/data/category");
		initCagegoryIds(categoryTreeString);
		System.out.println(CategoryUtil.getEduIds());
		System.out.println(CategoryUtil.getPeopleAttributes().toString());*/
	}

	private static List<Long> genderIds;
	private static List<Long> ageIds;
	private static List<Long> salaryIds;
	private static List<Long> jobIds;
	private static List<Long> eduIds;
	private static List<Long> keyStageIds;
	private static List<Long> regionIds;
	private static JSONArray peopleAttributes = JSONArray.fromObject("[]");

	private static void setGenderIds(List<Long> genderIds) {
		CategoryUtil.genderIds = genderIds;
	}

	private static void setSalaryIds(List<Long> salaryIds) {
		CategoryUtil.salaryIds = salaryIds;
	}

	private static void setJobIds(List<Long> jobIds) {
		CategoryUtil.jobIds = jobIds;
	}

	private static void setEduIds(List<Long> eduIds) {
		CategoryUtil.eduIds = eduIds;
	}

	private static void setKeyStageIds(List<Long> keyStageIds) {
		CategoryUtil.keyStageIds = keyStageIds;
	}

	private static void setRegionIds(List<Long> regionIds) {
		CategoryUtil.regionIds = regionIds;
	}

	private static void setAgeIds(List<Long> ageIds) {
		CategoryUtil.ageIds = ageIds;
	}

	public static void setPeopleAttributes(JSONArray peopleAttributes) {
		CategoryUtil.peopleAttributes = peopleAttributes;
	}

	public static List<Long> getGenderIds() {
		return genderIds;
	}

	public static List<Long> getAgeIds() {
		return ageIds;
	}

	public static List<Long> getSalaryIds() {
		return salaryIds;
	}

	public static List<Long> getJobIds() {
		return jobIds;
	}

	public static List<Long> getEduIds() {
		return eduIds;
	}

	public static List<Long> getKeyStageIds() {
		return keyStageIds;
	}

	public static List<Long> getRegionIds() {
		return regionIds;
	}

	public static JSONArray getPeopleAttributes() {
		return peopleAttributes;
	}

	public static void initCagegoryIds(String categoryTreeString) {

		JSONObject category = JSONObject.fromObject(categoryTreeString);
		// 10109是gender的根ID
		setGenderIds(getIdList(getChildren(category, "10109"), true));
		// 10112是年龄的根ID
		setAgeIds(getIdList(getChildren(category, "10112"), true));
		// 10119是月薪的根ID
		setSalaryIds(getIdList(getChildren(category, "10119"), true));
		// 10128是职业的根ID, 10133特殊处理
		setJobIds(getJobIdList(getChildren(category, "10128")));
		// 10137是学历的根ID
		setEduIds(getIdList(getChildren(category, "10137"), true));
		// 10144是关键人生阶段的根ID
		setKeyStageIds(getIdList(getChildren(category, "10144"), true));
		// 11988是region的根ID
		setRegionIds(getIdList(getChildren(category, "11988"), false));
	}

	private static JSONArray getChildren(JSONObject category, String idString) {
		if (idString.equalsIgnoreCase(category.get("id").toString())) {
			Object children = category.get("children");
			if (children != null && !(children instanceof JSONNull)) {
				JSONArray jsonArray = (JSONArray) children;
				return jsonArray;
			}
			return null;
		}
		Object children = category.get("children");
		if (children != null && !(children instanceof JSONNull)) {
			JSONArray jsonArray = (JSONArray) children;
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonObject = JSONObject.fromObject(jsonArray.get(i));
				JSONArray result = getChildren(jsonObject, idString);
				if (result != null) {
					return result;
				}
			}
		}
		return null;
	}

	private static List<Long> getIdList(JSONArray jsonArray, boolean setIntoAttributes) {
		List<Long> ids = new ArrayList<Long>();
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = JSONObject.fromObject(jsonArray.get(i));
			ids.add(Long.parseLong(jsonObject.get("id").toString()));
			if (setIntoAttributes) {
				JSONObject attribute = JSONObject.fromObject("{}");
				attribute.put("id", Long.parseLong(jsonObject.get("id").toString()));
				attribute.put("name", jsonObject.get("value").toString());
				peopleAttributes.add(attribute);
			}
		}
		return ids;
	}

	private static List<Long> getJobIdList(JSONArray jsonArray) {
		List<Long> ids = new ArrayList<Long>();
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = JSONObject.fromObject(jsonArray.get(i));
			if ("10133".equalsIgnoreCase(jsonObject.get("id").toString())) {
				JSONArray children = JSONArray.fromObject(jsonObject.get("children").toString());
				ids.addAll(getIdList(children, true));
			} else {
				ids.add(Long.parseLong(jsonObject.get("id").toString()));

				JSONObject attribute = JSONObject.fromObject("{}");
				attribute.put("id", Long.parseLong(jsonObject.get("id").toString()));
				attribute.put("name", jsonObject.get("value").toString());
				peopleAttributes.add(attribute);

			}
		}
		// 10133特殊处理
		return ids;
	}
}
